<template>
  <div class="page-container">
    <div
      class="main grid"
      :style="`--grid-template-columns: ${layout.col}; --grid-template-rows: ${layout.row}`"
    >
      <div
        class="section"
        v-for="(item, i) in layout.grids"
        :key="i"
        :style="`--grid-column: ${item.ax}; --grid-row: ${item.ay}`"
        :i="i"
      >
        <div
          class="section-item"
          v-for="(item2, ii) in pageConfig[i]"
          :key="ii"
        >
          <component
            ref="compId"
            :config="item2"
            :compKey="item2.componentKey"
            :children="item2.children"
            :data="list[item2.index]"
            :is="design[item2.componentKey]"
            @Form__Search="
              (comm, val) => {
                Form__Search(comm, val);
              }
            "
          >
          </component>
        </div>
      </div>
    </div>

    <CommonModal
      ref="modalRef"
      :title="modal ? modal.title : ''"
      :data="modal ? modal.attrs : []"
      @confirm="Confirm_Modal"
      @close="Close_Modal"
    ></CommonModal>
  </div>
</template>
<script setup>
import { SfeEes } from "@/apis/sfe/ees";
import { GtEej } from "@/apis/gt/eej";

/**
 *  页面配置对象
 */

const pageConfig = ref({
  0: [
    {
      search: true,
      reset: true,
      layout: "vertical",
      assign: null,
      componentKey: "form",
      index: "0-ac8727",
      children: [
        {
          componentKey: "input",
          index: "0-ac8727-9b1947",
          attrs: {
            label: "名称",
            field: "name",
            placeholder: "",
            value: "",
            allowClear: true,
            componentKey: "input",
            type: "Input",
            index: "0-ac8727-9b1947",
            required: false,
            rules: [],
          },
        },
        {
          componentKey: "select",
          index: "0-ac8727-e71d27",
          attrs: {
            label: "类型",
            field: "type",
            placeholder: "",
            value: null,
            allowClear: true,
            optionsLabel: "label",
            optionsValue: "value",
            options: GtEej,
            componentKey: "select",
            type: "Select",
            index: "0-ac8727-e71d27",
            required: false,
            rules: [],
          },
        },
        {
          componentKey: "datePicker",
          index: "0-ac8727-8b8535",
          attrs: {
            type: "DatePicker",
            label: "日期",
            field: "date",
            placeholder: "",
            value: null,
            valueFormat: "YYYY-MM-DD",
            allowClear: true,
            componentKey: "datePicker",
            index: "0-ac8727-8b8535",
            required: [1],
            rules: [
              {
                type: "string",
                required: true,
                message: "日期不能为空",
                trigger: "blur",
              },
            ],
          },
        },
      ],
      apiUrl: "/sfe/ees",
      rules: [],
    },
  ],
});

/**
 *  页面弹窗配置对象
 */

const modals = ref([]);

/**
 *  layout格子配置对象
 */

const layout = ref({
  col: "repeat(24, minmax(0, 1fr))",
  row: "repeat(94, minmax(0, 1fr))",
  grids: [
    {
      x: 0,
      y: 0,
      w: 24,
      h: 95,
      i: 1,
      moved: false,
      ax: "1 / 25",
      ay: "1 / 95",
      nm: "gm1",
    },
  ],
});

// 定义一个可变的响应式变量 compId，用于存储组件的ID
const compId = ref(null);
// 定义一个可变的响应式变量 modalRef，用于存储模态框的引用
const modalRef = ref(null);
// 定义一个可变的响应式变量 list，用于存储列表数据
const list = ref({});
// 定义 emit 函数，用于触发自定义事件
const emit = defineEmits();
// 定义一个可变的响应式变量 currentIndex，用于存储当前模态框的索引
const currentIndex = ref("");

// 定义一个计算属性 modal，用于根据 currentIndex 查找对应的模态框对象
const modal = computed(() => {
  // 在 modals 数组中查找索引匹配的模态框对象
  const foundModal = modals.value.find((a) => a.index === currentIndex.value);
  // 如果找到了匹配的模态框对象，则返回它
  if (foundModal) {
    return foundModal;
  } else {
    // 如果没有找到匹配的模态框对象，则在控制台输出警告信息
    console.warn(
      "No matching modal found for the current index:",
      currentIndex.value,
    );
    // 可以选择返回 null 或者一个默认的 modal 对象
    return null; // 或者返回一个默认的 modal 对象
  }
});
/**
 * 触发事件并传递参数
 *
 * 此函数用于在特定事件上触发数据的更新或处理逻辑它接受三个参数：
 * 事件名(event)，第一个参数(val)，和第二个参数(val2)通过调用emit函数，
 * 将这些参数传递给监听该事件的订阅者或处理器
 *
 * @param {string} event - 事件名称，用于标识特定的事件
 * @param {*} val - 第一个事件参数，可以是任意类型
 * @param {*} val2 - 第二个事件参数，同样可以是任意类型
 */
const change = (event, val, val2) => {
  emit(event, val, val2);
};
/**
 * 获取组件
 */
const design = ref({});

Object.keys(Designer).forEach(async (key) => {
  const Des = await Designer[key]();
  design.value[key] = Des.default;
});

const fetchSfeEes = async (val) => {
  try {
    const { code, data } = await SfeEes(val);
    if (code === rescf.codeVal) {
      message.success("操作成功！");
    }
  } catch (error) {
    console.error("Error fetching data:", error);
  }
};

const Form__Search = (comm, val) => {
  fetchSfeEes(val.formData);
};
</script>

<style lang="scss" scoped>
.flex {
  display: flex;
  flex-wrap: wrap;
}
.flex-1 {
  flex: 1;
}
.page-container {
  height: 100%;
  width: 100%;
  .main {
    margin: 0 auto;
    height: 100%;
    width: 100%;
    flex-direction: column;
    &.pc {
      width: 100%;
    }
    &.iphone {
      width: 480px;
    }
    &.grid {
      display: grid;
    }
  }
  .section {
    position: relative;
    width: 100%;
    grid-column: var(--grid-column);
    grid-row: var(--grid-row);
    min-width: 0px;
    display: flex;
    flex-direction: column;
    .section-item {
      &:last-child {
        flex: 1;
      }
    }
  }
  iframe {
    width: 100%;
    height: 100%;
  }
}
</style>
